package hu.elte.inf.holiuml.gui;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;

/**
 * @author Istvan Bozo
 */
public abstract class OptionsDialog extends JDialog {
	
	public static final int OK = 1;
	public static final int Cancel = 0;
	protected JButton okButton;
	protected JButton cancelButton;
	protected int buttonCode = Cancel;
	protected JPanel buttonPanel;
	protected Object obj = null;
	protected HoliUML parent;

	protected OptionsDialog(HoliUML parent, String title) {
		super(parent, title);
		this.parent = parent;
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		createButtonPanel();
		this.setLocation(new Point(parent.getLocation().x + 20,parent.getLocation().y + 100));
		getRootPane().setDefaultButton(okButton);
		setResizable(false);
	}

	private void createButtonPanel() {
		okButton = new JButton("OK");
		okButton.addActionListener(okAction);
		okButton.setPreferredSize(new Dimension(90, 25));
		cancelButton = new JButton("Cancel");
		cancelButton.addActionListener(cancelAction);
		cancelButton.setPreferredSize(new Dimension(90, 25));
		buttonPanel = new JPanel(new FlowLayout());
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
	}

	private ActionListener okAction = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			if (processOK()) {
				OptionsDialog.this.dispose();
				buttonCode = OK;
			}
		}
	};

	private ActionListener cancelAction = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			OptionsDialog.this.dispose();
			buttonCode = Cancel;
		}
	};

	public int getButtonCode() {
		return buttonCode;
	}

	protected abstract boolean processOK();

	protected abstract void processCancel();
}
